package com.tjian.fafa.user.base.mainBody;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.ConnectedApp;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.base.commons.GlobalMsgTypes;
import com.tjian.fafa.user.base.myNetwork.NetworkService;

public class MainMyDataPageActivity extends Activity
{
    private static MainMyDataPageActivity mInstance;
    private Context mContext0;
    private Spinner mSpBirthYear;
    private Spinner mSpBirthMonth;
    private Spinner mSpBirthDay;
    private RadioGroup mRgpSex;
    private Button mBtnUpdate;
    private int stYear = 1941;

    public static MainMyDataPageActivity getInstance()
    {
        if (mInstance == null)
        {
            mInstance = new MainMyDataPageActivity();
        }
        return mInstance;
    }

    public MainMyDataPageActivity()
    {
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.update_my_data);
        mSpBirthYear = (Spinner) findViewById(R.id.tabsettings_spinner_year);
        mSpBirthMonth = (Spinner) findViewById(R.id.tabsettings_spinner_month);
        mSpBirthDay = (Spinner) findViewById(R.id.tabsettings_spinner_day);
        mRgpSex = (RadioGroup) findViewById(R.id.tabsettings_rgp_chooseSex);
        mBtnUpdate = (Button) findViewById(R.id.tabsettings_btn_update);
        UserInfo me = ConnectedApp.getInstance().getUserInfo();
        mSpBirthYear.setSelection(me.getBirthYear() - stYear);
        mSpBirthMonth.setSelection(me.getBirthMonth() - 1);
        mSpBirthDay.setSelection(me.getBirthDay() - 1);
        if (me.getSex() == 0)
        {
            mRgpSex.check(R.id.tabsettings_rd_btnFemale);
        }
        else
        {
            mRgpSex.check(R.id.tabsettings_rd_btnMale);
        }
        mBtnUpdate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                int birthYear = mSpBirthYear.getSelectedItemPosition() + 1941;
                int birthMonth = mSpBirthMonth.getSelectedItemPosition() + 1;
                int birthDay = mSpBirthDay.getSelectedItemPosition() + 1;

                int sex;
                int sexChoseId = mRgpSex.getCheckedRadioButtonId();
                switch (sexChoseId)
                {
                    case R.id.tabsettings_rd_btnFemale:
                        sex = 0;
                        break;
                    case R.id.tabsettings_rd_btnMale:
                        sex = 1;
                        break;
                    default:
                        sex = -1;
                        break;
                }
                if (sex == -1)
                {
                    Toast.makeText(mContext0, "请选择性别", Toast.LENGTH_SHORT).show();
                    return;
                }
                UserInfo me = ConnectedApp.getInstance().getUserInfo();
                me.setBirthYear(birthYear);
                me.setBirthMonth(birthMonth);
                me.setBirthDay(birthDay);
                me.setSex(sex);
                NetworkService.getInstance().sendUpload(GlobalMsgTypes.msgUpdateUserInfo, me.toString());
                ConnectedApp.getInstance().setUserInfo(me);
                Toast.makeText(mContext0, "修改成功！", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
